<?php
namespace Org\Util;
class ExecSql {
	/**
	 * 自动执行sql建表语句
	 */
	/**使用

    $columnArr = array(
            array('COLUMNNAME'=>'appid', 'TYPE'=>'varchar(18)', 'DEFAULT'=>'NULL', 'COMMENT'=>'公众号标识'),
            array('COLUMNNAME'=>'appsecret', 'TYPE'=>'varchar(32)', 'DEFAULT'=>'NULL', 'COMMENT'=>'公众号秘钥'),
            array('COLUMNNAME'=>'starttime', 'TYPE'=>'int(10)', 'DEFAULT'=>'0', 'COMMENT'=>'项目开始时间'),
            array('COLUMNNAME'=>'endtime', 'TYPE'=>'int(10)', 'DEFAULT'=>'0', 'COMMENT'=>'项目结束时间'),
            array('COLUMNNAME'=>'wxShareLink', 'TYPE'=>'varchar(200)', 'DEFAULT'=>'NULL', 'COMMENT'=>'分享地址'),
            array('COLUMNNAME'=>'wxShareImgUrl', 'TYPE'=>'varchar(200)', 'DEFAULT'=>'NULL', 'COMMENT'=>'分享图片'),
            array('COLUMNNAME'=>'wxShareDesc', 'TYPE'=>'varchar(200)', 'DEFAULT'=>'NULL', 'COMMENT'=>'分享内容'),
            array('COLUMNNAME'=>'wxShareTitle', 'TYPE'=>'varchar(100)', 'DEFAULT'=>'NULL', 'COMMENT'=>'分享标题'),
            array('COLUMNNAME'=>'status', 'TYPE'=>'tinyint(2)', 'DEFAULT'=>'1', 'COMMENT'=>'启用状态'),
            array('COLUMNNAME'=>'author', 'TYPE'=>'varchar(35)', 'DEFAULT'=>'NULL', 'COMMENT'=>'创建者'),
            array('COLUMNNAME'=>'create_time', 'TYPE'=>'int(10)', 'DEFAULT'=>'0', 'COMMENT'=>'创建时间'),
        );

    $config = array(
        'NAME' => '表名',
        'E_NAME' => 'tablename_config',
        'COMMENT' => '表明+项目表',
        'COLUMN' => $columnArr,
    );

    $ExecSql = new \Org\Util\ExecSql($config);
    $res = $ExecSql->createTable();
    p($res);

    **/
	

	private $config = array(
        'PRIMARY KEY'        =>  'id', //主键名称
    );

    private $DB_PREFIX;

    /**
     * 错误信息
     * @var string
     */
    private $error = ''; //错误信息

    public function __construct($config = array()){
        /* 获取配置 */
        // $this->config   =   array_merge($this->config, $config);
        $config['PRIMARY KEY'] = $config['PRIMARY KEY'] ? $config['PRIMARY KEY'] : $this->config['PRIMARY KEY']; 
        $this->config   =   $config;
        $this->DB_PREFIX = C("DB_PREFIX");
    }

    /**
     * 使用 $this->name 获取配置
     * @param  string $name 配置名称
     * @return multitype    配置值
     */
    public function __get($name) {
        return $this->config[$name];
    }

    public function __set($name,$value){
        if(isset($this->config[$name])) {
            $this->config[$name] = $value;
        }
    }

    public function __isset($name){
        return isset($this->config[$name]);
    }

    /**
     * 获取错误信息
     * @return string 错误信息
     */
    public function getError(){
        return $this->error;
    }

    /**
     * 自动创建表
     */
    public function createTable() {
        $config = $this->config;   
        // p($config);
        $this->execCreate($config);

    }

    /**
     * 执行建表语句
     * @param  array $config 配置信息
     * @return true
     */
    public function execCreate($config) {

        if(!$config) return false;

        $DB_PREFIX = $this->DB_PREFIX;
        
        $table_name = $DB_PREFIX.$config['E_NAME'];
        $table_comment = $config['COMMENT'];
        $primary_key  = $config['PRIMARY KEY'];

        $model = M();
        $sql[] = "CREATE TABLE IF NOT EXISTS `{$table_name}` ( `{$primary_key}` int(11) NOT NULL AUTO_INCREMENT";
        foreach ($config['COLUMN'] as $k => $data) {
            $sql[] = "`{$data['COLUMNNAME']}` {$data['TYPE']} DEFAULT {$data['DEFAULT']} COMMENT '{$data['COMMENT']}'";
        }

        $sql[] = "PRIMARY KEY(`{$primary_key}`)  ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='{$table_comment}'";
        $sql = arr2str($sql);

        $res = $model->execute($sql);

        if($res) return 1;
        
    }


}
?>